package org.pnp.main;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.pnp.db.*;
public class StockInfo extends Activity {
	
	private StockDBAdapter dbHelper = null;
	
	private EditText stockCodeEditText;
	private EditText stockNameEditText;
	private EditText highWarnPriceEditText;
	private EditText lowWarnPriceEditText;
	private TextView row_id;
	private Long mRowId;
	private Button saveButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stock_detail);;
		this.row_id = (TextView) findViewById(R.id.RowId_TextView01);
		this.stockCodeEditText = (EditText)findViewById(R.id.stock_edit_code);
		this.stockNameEditText = (EditText)findViewById(R.id.stock_edit_name);
		this.highWarnPriceEditText = (EditText)findViewById(R.id.Stock_Edit_Up_Warn_Price);
		this.lowWarnPriceEditText = (EditText)findViewById(R.id.Stock_Edit_Down_Warn_Price);
		this.saveButton = (Button)findViewById(R.id.save_edit_button);
		mRowId = null;
		Bundle extras = getIntent().getExtras();
		mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState
				.getSerializable(dbHelper.KEY_ROWID);
		if (extras != null) {
			mRowId = extras.getLong(dbHelper.KEY_ROWID);
		}


		dbHelper = new StockDBAdapter(this);
		dbHelper = dbHelper.open();
		
		//populate the stock detail information here
		populateStockInfo();
		this.saveButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View view){
				setResult(RESULT_OK);
				finish();
			}
		});
	}
	
	private void populateStockInfo(){
		if(this.mRowId != null){
			Cursor cur = dbHelper.fetchStock(mRowId);
			startManagingCursor(cur);
			if(cur.getCount() > 0){
				this.stockCodeEditText.setText(cur.getString(cur.getColumnIndexOrThrow(dbHelper.KEY_STOCKCODE)));
				this.stockNameEditText.setText(cur.getString(cur.getColumnIndexOrThrow(dbHelper.KEY_STOCKNAME)));
				this.highWarnPriceEditText.setText(String.valueOf(cur.getDouble(cur.getColumnIndexOrThrow(dbHelper.KEY_UPWARNPRICE))));
				this.lowWarnPriceEditText.setText(String.valueOf(cur.getDouble(cur.getColumnIndexOrThrow(dbHelper.KEY_DOWNWARNPRICE))));
			}
			
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		saveState();
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateStockInfo();
	}

	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(dbHelper.KEY_ROWID, mRowId);
	}

	private void saveState(){
		String stockCode = this.stockCodeEditText.getText().toString();
		String stockName = this.stockNameEditText.getText().toString();
		double highWarnPrice = Double.parseDouble(this.highWarnPriceEditText.getText().toString());
		double lowWarnPrice = Double.parseDouble(this.lowWarnPriceEditText.getText().toString());
		//set yesterday closed price as 0
		if(this.row_id.getText() == null || this.row_id.getText() == ""){
		dbHelper.createStock(stockCode, stockName, 0, highWarnPrice, lowWarnPrice);
		}else{
			long rowId = Long.parseLong(this.row_id.getText().toString());
			dbHelper.updateStock(rowId, stockCode, stockName, 0, highWarnPrice, lowWarnPrice);
		}
		//dbHelper.close();
	}

}
